{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "162F72E1FEB04F09993F9E3770737F64",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "# Lecture 3: Conditional Probability and Bayes rule \n",
                "\n",
                "## Instructor： 胡传鹏（博士）[Dr. Hu Chuan-Peng]\n",
                "\n",
                "### 南京师范大学心理学院[School of Psychology, Nanjing Normal University]\n",
                " \n",
                "## Part 1: Conditional Probability (条件概率)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "DDAD13227109442F8733374747B7C5A8",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "## Recap of previous lecture\n",
                "\n",
                "### 计算与概率\n",
                "### 证据更新与贝叶斯法则\n",
                "### 贝叶斯与频率学派对比"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "9FF68215A65049429BBF76C422538947",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "\n",
                "![Image Name](https://cdn.kesci.com/upload/image/rhqd6akbc6.gif?imageView2/0/w/640/h/640)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "DB6B10B396A54FE9AC9BAFB5A80192B0",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "|                     | 频率学派   | 贝叶斯学派   |\n",
                "| ------------------- | ---------- | ------------ |\n",
                "| 世界真相 (参数) | 固定       | 变化         |\n",
                "| 概率                | 抽样的噪音 | 信念         |\n",
                "| 推断过程            | NHST       | 贝叶斯定理   |\n",
                "| 数据                | 存在噪音   | 固定         |\n",
                "| 推断可更新性        | 否         | 是           |\n",
                "| 主观性              | 前提预设   | 通过先验设定 |"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "C9DFD16782FA419D9828E6164CFD2CE2",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "#### 贝叶斯公式：\n",
                "####  $Posterior = \\frac {probability \\, of \\, data \\, * \\, prior}{Average \\, probability \\, of \\, data}$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "7993448A9CF746118E7E0E33E618DB50",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "#### $P(A|B) = \\frac{P(A) * P(B|A)} {P(B)}$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "FFC84883006440E1A86979C97E62C714",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "#### Core concept today:\n",
                "\n",
                "**$P(A|B)$**"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "2503087FB27546EDB9C6C32EB70ED82C",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "在已知其他事件发生的前提下，我们想知道某个事件发生的概率有多大，这就是所谓的条件概率。\n",
                "\n",
                "关注的焦点是样本空间的一个子集，这类概率被称为条件概率，记作$P(A|B)$或$Pr(A|B)$，读作“已知B时A的概率”。"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "7A60B6E686CE446BAC5AA1898EC5039E",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "在生活中，条件概率随处可见：\n",
                "- 对一个65岁且不抽烟的人来说，他得肺癌的概率是多少？\n",
                "- 清晨，发现窗外的马路是湿的，昨晚下过雨的概率是多少（在秋季的南京）\n",
                "- 清晨，发现窗外的马路是湿的，昨晚下过雨的概率是多少（在秋季的新疆）"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "52A698B93D854DAC9C2788097B23BB8E",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "## 计数与条件概率"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "E380767F769F4C758441C825979AE4F2",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "某资深渔友，去一处自己熟悉的河流垂钓，并制定了一个计划：一旦钓到鱼或者等待了4个小时，就停止钓鱼。\n",
                "\n",
                "做如何假定：\n",
                "* 该河钓到鲫鱼的概率是40%；\n",
                "* 钓到鲈鱼的概率是25%；\n",
                "* 钓不到鱼的概率是35%\n",
                "\n",
                "*三个概率的和是1，概率公理之一：整体样本集合中的某个基本事件发生的概率为1*\n",
                "\n",
                "假设，该渔友在4小时内钓到了一条鱼。\n",
                "\n",
                "问题：这条鱼恰好是鲫鱼的概率是多少？"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "4192A0CDA5434DF5A65F7D7CF2412D0F",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "通过计数来解决概率问题：\n",
                "\n",
                "假定该渔友不知疲倦，去该河进行了1000垂钓，则\n",
                "* 400次是鲫鱼；\n",
                "* 250次钓到鲈鱼；\n",
                "* 350次没有收获。\n",
                "\n",
                "在上述情景下：钓到一次鱼的总次数是400+250 = 650，其中\n",
                "* 400次是鲫鱼，\n",
                "\n",
                "那么，钓到一条鱼且该鱼为鲫鱼的可能性是：$\\frac{400}{650} \\approx 61.5\\% $。"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "064B49883C7B4D128D60A116795A11FE",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "我们将A表示钓到了一条鲫鱼，B表示钓到了鱼，$P(A)$和$P(B)$分别来表示钓到鲫鱼和钓到鱼的概率。\n",
                "\n",
                "那么$P(A)=0.4$，$P(B)=0.4+0.25=0.65$。\n",
                "\n",
                "如果我们钓到了一条鲫鱼，说明我们钓到了鱼，也就是说当事件A发生时，事件B已经发生了，即$A \\in B$.\n",
                "\n",
                "我们将这个概率定义为$P(A \\cap B)= P(A) = 0.4$。\n",
                "\n",
                "$P(A|B) = \\frac {P(A \\cap B)} {P(B)} = \\frac {0.4} {0.65}$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "72A955A523A34A8587B82E45D0F983BE",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "我们是否可以反过来？\n",
                "\n",
                "$P(B|A) = ?$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "8165EBF7047F474BAE2EB04108F069E6",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "### 条件概率与疾病诊断"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "07C73CBDB7B14538B15334F8E5F7BDF3",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "在我国，每100人有3人患抑郁症。\n",
                "\n",
                "假设小明情绪低落，去医院检查是否患抑郁症，医生告诉小明，抑郁症检测出现假阳性的概率是1%，也就是说每100个健康人中会有一个人的测试为阳性。医生还告诉小明，这个测试假阴性率为0.1%，即每1000抑郁症患者中，只有一人会被检测为阴性。假设小明检测为阳性，那么得抑郁症的概率是多少？"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "88C159B351D8430D98C79919E9A64D21",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "已知：\n",
                "$$\n",
                "P(阳性｜抑郁) = 1 - P(阴性 |抑郁) = 1 - 0.1\\% = 99.9\\%\n",
                "$$\n",
                "\n",
                "\n",
                "$$\n",
                "P(阳性｜健康) = 1\\%\n",
                "$$\n",
                "\n",
                "$$\n",
                "P(抑郁) = 3\\%\n",
                "$$\n",
                "\n",
                "求：\n",
                "$$\n",
                "P(抑郁|阳性)\n",
                "$$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "EDF8FB63B27D4CBE92B354500194EC29",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "我们可以假设现在有100000人，这样设定是来自问题中100人中有3人患有抑郁症，我们可以通过树状图看到更形象的展示：\n",
                "\n",
                "![Image Name](https://cdn.kesci.com/upload/image/ri8ls9vvdh.jpg?imageView2/0/w/960/h/960)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "455FC1968F804E868B9FF503F9C97B27",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "所以当小明检测为阳性，又患抑郁症的概率为\n",
                "$$\n",
                "P(抑郁| 阳性) = \\frac {抑郁且阳性的人数}{抑郁且阳性的人数 + 抑郁但健康的人数} = \\frac{2997}{970+2997} \\approx 0.755\n",
                "$$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "2B70F943E7E647C9A6B449451AEAC9E9",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "如果我们用条件概率的知识，我们已知$P(阳性|健康)$和$P(阴性|抑郁)$，希望求出$P(抑郁|阳性)$，我们可以根据上面的例子得到："
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "845CF227E76445FAB51172740FF08C88",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "$$\n",
                "P(抑郁|阳性)=\\frac{P(抑郁)\\times P(阳性|抑郁)} {P(阳性)}\n",
                "$$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "516145F727264CCFAD342590480FC2C1",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "这就是我们说的贝叶斯定理，或者贝叶斯公式"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "2FCE561E013F4D99AB929B00BD7F0FC1",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "<div style=\"text-align: center;\">\t\n",
                "\n",
                "# Lecture 3: Conditional Probability and Bayes rule \n",
                "## Instructor： 胡传鹏（博士）[Dr. Hu Chuan-Peng]\n",
                "### 南京师范大学心理学院[School of Psychology, Nanjing Normal University]\n",
                " \n",
                "## Part 2: Parameters (模型参数)\n",
                "\t\n",
                "</div>"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "3F775D9DB66048ECA9BF0973FD9F4EF1",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "\n",
                "### 概率质量函数 (Probability Mass Function, PMF)\n",
                "\n",
                "离散随机变量X的概率质量函数被定义为随机变量呈现特定值的概率。\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "CC0A5CFAC47D4C54AEDD4AF7C6006900",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "$p(x_k)=P[X=x_k]$\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "1489B36C3C0949A49B279AB4B4683894",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "### 概率密度函数 (Probability Density Function, PDF)\n",
                "\n",
                "随机变量的取值落在某个区域之内的概率则为概率密度函数在这个区域上的积分"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "04C654E41084453A95DDDDDE5502CEAA",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "\n",
                "$f(x)=\\sum_{k}^{}p(x_k)\\delta(x-x_k)$\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "A92ED4DB777C4333B9902D6F946A4236",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "### 离散概率分布"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "DDC236CF5F8C4CE788778BD7E3929969",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "#### 伯努利分布(Bernoulli distribution)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "FDA1F7E11F8049EA9E7E81AAE01989B0",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "假设一个事件只有发生或者不发生两种可能，并且这两种可能是固定不变的。那么，如果假设它发生的概率是$p$，那么它不发生的概率就是$q = 1-p$。\n",
                "\n",
                "\n",
                "伯努利实验就是做一次服从伯努利概率分布的事件，它发生的可能性是$p$，不发生的可能性是$1-p$。"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "769D43364CD54BE4AE224BC7E9BED9A3",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                " $f(k,p) = p^{k}(1-p)^{1-k}$\n",
                "\n",
                "for $k \\in {0, 1} $ $k$ 为可能的结果"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "CCF58A864E3944DC9369EC24D3F0FC1D",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "如何在Python中观察这种分布？"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {
                "collapsed": false,
                "id": "8E5111549DD14DF9AC065029A11093D0",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [],
            "source": [
                "import numpy as np\n",
                "import matplotlib.pyplot as plt\n",
                "\n",
                "# 从scipy.stats模块加载二项分布、伯努利分布、正态分布、超几何分布、泊松分布、t分布模块\n",
                "from scipy.stats import binom, bernoulli, norm, hypergeom, poisson, t\n",
                "# 从scipy包加载stats模块\n",
                "from scipy import stats"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {
                "collapsed": false,
                "id": "4BE808FF996B447E951134E5B7DDF4C9",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/4BE808FF996B447E951134E5B7DDF4C9/ri5dlich5y.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "fig, ax = plt.subplots(1, 1)\n",
                "\n",
                "p = 0.3\n",
                "\n",
                "# 在参数为0.3的情况下，伯努利分布的平均值m、方差v、峰度s和偏度k\n",
                "mean, var, skew, kurt = bernoulli.stats(p, moments='mvsk') \n",
                "\n",
                "x1 = bernoulli.ppf(0.01, p)\n",
                "x2 = bernoulli.ppf(0.99, p)\n",
                "\n",
                "ax.plot(x1, bernoulli.pmf(x1, p), 'bo', ms=8, label='bernoulli pmf')\n",
                "\n",
                "ax.vlines(x1, 0, bernoulli.pmf(x1, p), colors='b', lw=5, alpha=0.5)\n",
                "\n",
                "ax.plot(x2, bernoulli.pmf(x2, p), 'bo', ms=8, label='bernoulli pmf')\n",
                "\n",
                "ax.vlines(x2, 0, bernoulli.pmf(x2, p), colors='b', lw=5, alpha=0.5)\n",
                "\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "metadata": {
                "collapsed": false,
                "id": "697B14B79C5342678F712935E6221713",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array(0.3)"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "mean"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 14,
            "metadata": {
                "collapsed": false,
                "id": "6278ADB668694D3282E5E8491413458C",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [],
            "source": [
                "p = 0.3"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 15,
            "metadata": {
                "collapsed": false,
                "id": "6C7A468276D5496B8BCE8A94480AE2F6",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [],
            "source": [
                "mean, var, skew, kurt = bernoulli.stats(p, moments='mvsk') "
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 16,
            "metadata": {
                "collapsed": false,
                "id": "C7B8E264FE814ADF8DCA121BF04166C1",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array(0.3)"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "mean"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 17,
            "metadata": {
                "collapsed": false,
                "id": "F64D9DAA98034B3EA517089FABC1AF77",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array(0.21)"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "var"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 18,
            "metadata": {
                "collapsed": false,
                "id": "133588490EFD452E86EA7AE023796A72",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array(0.87287156)"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "skew"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 19,
            "metadata": {
                "collapsed": false,
                "id": "5F91B0FAAD534BE4884218F9AC1D3EA2",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array(-1.23809524)"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "kurt"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "C240744D7F9E40A88CEF80B2D9A040C7",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "#### 二项分布(Binomial distribution)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "205780687709460B8142211D85385FCE",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "二项分布是多次伯努利分布实验的概率分布。\n",
                "\n",
                "以抛硬币举例，在抛硬币事件当中，每一次抛硬币的结果是独立的，并且每次抛硬币正面朝上的概率是恒定的，所以单次抛硬币符合伯努利分布。我们假设硬币正面朝上的概率是p，忽略中间朝上的情况，那么反面朝上的概率是q=(1-p)。我们重复抛n次硬币，其中有k项正面朝上的事件，就是二项分布"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "7170463B7A79413887B0EAF041523B8A",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "$b(k,n,p) = \\binom{k}{n} p^{k}(1-p)^{n-k}$"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 3,
            "metadata": {
                "collapsed": false,
                "id": "AF5489613B1C4F4F9A701D0168BDEF94",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/AF5489613B1C4F4F9A701D0168BDEF94/ri58lkeqrb.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "fig, ax = plt.subplots(1, 1)\n",
                "n, p = 5, 0.4\n",
                "\n",
                "# 在参数n为5，p为0.4的情况下，伯努利分布的平均值m、方差v、峰度s和偏度k\n",
                "mean, var, skew, kurt = binom.stats(n, p, moments='mvsk')\n",
                "\n",
                "x = np.arange(binom.ppf(0.01, n, p),\n",
                "              binom.ppf(0.99, n, p))\n",
                "\n",
                "ax.plot(x, binom.pmf(x, n, p), 'bo', ms=8, label='binom pmf')\n",
                "\n",
                "ax.vlines(x, 0, binom.pmf(x, n, p), colors='b', lw=5, alpha=0.5)\n",
                "\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "52D00CBD570143C3A2E36E287545AD91",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "<div style=\"text-align: center;\">\n",
                "\n",
                "##### 二项分布(bonimial distribution)，当n取值不同时\n",
                "\n",
                "</div>"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 13,
            "metadata": {
                "collapsed": false,
                "id": "5A962DF6CE844BF6A09ABF34F0303AF7",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "<BarContainer object of 16 artists>"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/5A962DF6CE844BF6A09ABF34F0303AF7/ri58pwp8i1.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# p=0.5,n=5\n",
                "p = 0.5\n",
                "k = np.linspace(0,5,6)\n",
                "n = 5\n",
                "\n",
                "prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.5)\n",
                "\n",
                "# p=0.5, n=10\n",
                "\n",
                "p = 0.5\n",
                "k = np.linspace(0,10,11)\n",
                "n = 10\n",
                "\n",
                "prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.5)\n",
                "\n",
                "# p=0.5, n=15\n",
                "\n",
                "p = 0.5\n",
                "k = np.linspace(0,15,16)\n",
                "n = 15\n",
                "\n",
                "prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.5)\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "9E946B65A5984975AFAE6BACC58FA056",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "##### 二项分布(bonimial distribution)，当p取值不同时"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 14,
            "metadata": {
                "collapsed": false,
                "id": "F9EE8900886C4D9D90D4FE20CCE5C43E",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "<BarContainer object of 11 artists>"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/F9EE8900886C4D9D90D4FE20CCE5C43E/ri58r2i5cx.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# p = 0.3, n=10\n",
                "p = 0.3\n",
                "k = np.linspace(0,10,11)\n",
                "n = 10\n",
                "\n",
                "prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.5)\n",
                "\n",
                "\n",
                "# # p = 0.4, n=10\n",
                "# p = 0.4\n",
                "# k = np.linspace(0,10,11)\n",
                "# n = 10\n",
                "\n",
                "# prob = binom.pmf(k,n,p)\n",
                "\n",
                "# plt.bar(k,prob,width=1,alpha=0.6)\n",
                "\n",
                "# p = 0.5, n=10\n",
                "p = 0.5\n",
                "k = np.linspace(0,10,11)\n",
                "n = 10\n",
                "\n",
                "prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.6)\n",
                "\n",
                "# # p = 0.6, n=10\n",
                "# p = 0.6\n",
                "# k = np.linspace(0,10,11)\n",
                "# n = 10\n",
                "\n",
                "# prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.5)\n",
                "p = 0.7\n",
                "k = np.linspace(0,10,11)\n",
                "n = 10\n",
                "\n",
                "prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.5)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "E78E4FBCD3254C8EAE93CBE38D67947D",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "### 超几何分布\n",
                "\n",
                "例如在有$N$个样本，其中$K$个是不及格的。超几何分布描述了在该$N$个样本中抽出 $n$个，其中 $k$个是不及格的机率："
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "EFA6C9D7338A4872811FD81C25EA24C0",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                " $p(k,n,K,N)=\\frac{\\binom{K}{k}\\binom{N-K}{n-k} }{\\binom{N}{n} }$\n",
                "\n",
                " $\\binom{n}{k} = \\frac{n!}{k!(n-k)!}$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "15DBEB44E0964B95A88A3ABA3D8D33E6",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "假设某医院接收到20位病人，其中7位病人患有心理健康问题。那么，如果我们想知道如果我们从20位病人中中随机选择12位，找到一定数量的患有精神健康问题的病人的概率。"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 6,
            "metadata": {
                "collapsed": false,
                "id": "5287AF6409EA42309D71EEBBEE9FF27E",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/5287AF6409EA42309D71EEBBEE9FF27E/ri58lk7w3r.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "[M, n, N] = [20, 7, 12]\n",
                "\n",
                "rv = hypergeom(M, n, N)\n",
                "\n",
                "x = np.arange(0, n+1)\n",
                "\n",
                "pmf_target = rv.pmf(x)\n",
                "\n",
                "fig = plt.figure()\n",
                "\n",
                "ax = fig.add_subplot(111)\n",
                "\n",
                "ax.plot(x, pmf_target, 'bo')\n",
                "\n",
                "ax.vlines(x, 0, pmf_target, lw=2)\n",
                "\n",
                "ax.set_xlabel('# of target in our group of chosen people')\n",
                "\n",
                "ax.set_ylabel('hypergeom PMF')\n",
                "\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "34D055520E24418F8D99037B805D6111",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "<div style=\"text-align: center;\">\n",
                "\n",
                "# 泊松分布\n",
                "\n",
                "</div>"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "CBDAA867FD49417F991EE6003A1A6213",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "$f(k)=exp(-\\mu)\\frac{\\mu^k}{k!}$"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "E1BF0088CA6D4406ABDC673D82E32483",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "某神经元在一段时间内平均放电6次。我们想要知道在下一段同样的时间内，该神经元会放电多少次。"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 7,
            "metadata": {
                "collapsed": false,
                "id": "5AA43E706B0745D286B57E515E5E7268",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/5AA43E706B0745D286B57E515E5E7268/ri58lksow1.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "fig, ax = plt.subplots(1, 1)\n",
                "mu = 6\n",
                "mean, var, skew, kurt = poisson.stats(mu, moments='mvsk')\n",
                "x = np.arange(poisson.ppf(0.01, mu),\n",
                "              poisson.ppf(0.99, mu))\n",
                "ax.plot(x, poisson.pmf(x, mu), 'bo', ms=8, label='poisson pmf')\n",
                "ax.vlines(x, 0, poisson.pmf(x, mu), colors='b', lw=5, alpha=0.5)\n",
                "\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "6DBD296116C840128F4053E3E474C543",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "### 连续概率分布"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "7E42C58FD54145A79D28FCF79E7756B9",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "#### 二项分布，当n-> $\\infty$\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 8,
            "metadata": {
                "collapsed": false,
                "id": "88D38C49FD9545F3967F8182C8FF0516",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "<BarContainer object of 1001 artists>"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/88D38C49FD9545F3967F8182C8FF0516/ri58llsqkm.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "p = 0.5\n",
                "k = np.linspace(0,1000,1001)\n",
                "n = 1000\n",
                "\n",
                "prob = binom.pmf(k,n,p)\n",
                "\n",
                "plt.bar(k,prob,width=1,alpha=0.6)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "B1AB5DE520234E1E8718F74B3AA53AE0",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "当二项分布的参数你趋近于无穷大时，二项分布近似正态分布"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "4ECE3471FA7B49C09C525A0B0B9006C8",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "<div style=\"text-align: center;\">\n",
                "\n",
                "# 正态分布(Normal distribution)\n",
                "\n",
                "</div>"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "7264484A400E46558BE31E05D58D579E",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "$x \\sim N(\\mu, \\sigma^2)$\n",
                "\n",
                "\n",
                "$f(x) = \\frac{1}{\\sigma \\surd {2\\pi}}e^{-\\frac{(x-\\mu)^2}{2\\sigma^2}}$"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 9,
            "metadata": {
                "collapsed": false,
                "id": "CBAC503978E0448FB7A9731538BE67F0",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/CBAC503978E0448FB7A9731538BE67F0/ri58lmrmfm.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "fig, ax = plt.subplots(1, 1)\n",
                "mean, var, skew, kurt = norm.stats(moments='mvsk')\n",
                "\n",
                "x = np.linspace(norm.ppf(0.01),\n",
                "                norm.ppf(0.99), 100)\n",
                "ax.plot(x, norm.pdf(x),\n",
                "       'r-', lw=5, alpha=0.6, label='norm pdf')\n",
                "rv = norm()\n",
                "ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf')\n",
                "\n",
                "\n",
                "vals = norm.ppf([0.001, 0.5, 0.999])\n",
                "np.allclose([0.001, 0.5, 0.999], norm.cdf(vals))\n",
                "\n",
                "r = norm.rvs(size=1000)\n",
                "ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)\n",
                "plt.show()"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 20,
            "metadata": {
                "collapsed": false,
                "id": "C642C4D146BA4774823FC7E2A333CDC2",
                "jupyter": {},
                "notebookId": "6320589d9caeb667564a577a",
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "array([-2.32634787, -2.27935095, -2.23235402, -2.18535709, -2.13836017,\n",
                            "       -2.09136324, -2.04436631, -1.99736939, -1.95037246, -1.90337553,\n",
                            "       -1.85637861, -1.80938168, -1.76238475, -1.71538783, -1.6683909 ,\n",
                            "       -1.62139397, -1.57439705, -1.52740012, -1.48040319, -1.43340627,\n",
                            "       -1.38640934, -1.33941241, -1.29241549, -1.24541856, -1.19842163,\n",
                            "       -1.15142471, -1.10442778, -1.05743085, -1.01043393, -0.963437  ,\n",
                            "       -0.91644007, -0.86944314, -0.82244622, -0.77544929, -0.72845236,\n",
                            "       -0.68145544, -0.63445851, -0.58746158, -0.54046466, -0.49346773,\n",
                            "       -0.4464708 , -0.39947388, -0.35247695, -0.30548002, -0.2584831 ,\n",
                            "       -0.21148617, -0.16448924, -0.11749232, -0.07049539, -0.02349846,\n",
                            "        0.02349846,  0.07049539,  0.11749232,  0.16448924,  0.21148617,\n",
                            "        0.2584831 ,  0.30548002,  0.35247695,  0.39947388,  0.4464708 ,\n",
                            "        0.49346773,  0.54046466,  0.58746158,  0.63445851,  0.68145544,\n",
                            "        0.72845236,  0.77544929,  0.82244622,  0.86944314,  0.91644007,\n",
                            "        0.963437  ,  1.01043393,  1.05743085,  1.10442778,  1.15142471,\n",
                            "        1.19842163,  1.24541856,  1.29241549,  1.33941241,  1.38640934,\n",
                            "        1.43340627,  1.48040319,  1.52740012,  1.57439705,  1.62139397,\n",
                            "        1.6683909 ,  1.71538783,  1.76238475,  1.80938168,  1.85637861,\n",
                            "        1.90337553,  1.95037246,  1.99736939,  2.04436631,  2.09136324,\n",
                            "        2.13836017,  2.18535709,  2.23235402,  2.27935095,  2.32634787])"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                }
            ],
            "source": [
                "x = np.linspace(norm.ppf(0.01),\n",
                "                norm.ppf(0.99), 100)\n",
                "x"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "998A4DFAF7044AD59CAD381A83522ED7",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "<div style=\"text-align: center;\">\n",
                "\n",
                "# 正态分布(Normal distribution)，当$\\mu$不同时\n",
                "\n",
                "</div>"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 10,
            "metadata": {
                "collapsed": false,
                "id": "741989C964124F04B6A94DC5C7ABA256",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "<BarContainer object of 20 artists>"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/741989C964124F04B6A94DC5C7ABA256/ri58lmjxdn.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# mu = 0，sigma = 1\n",
                "x= np.linspace(-3,3,20)\n",
                "prob=norm.pdf(x,0,1)\n",
                "\n",
                "plt.bar(x,prob,width=0.4,alpha=0.6)\n",
                "\n",
                "# mu = 1，sigma = 1\n",
                "x= np.linspace(-3,3,20)\n",
                "prob=norm.pdf(x,1,1)\n",
                "\n",
                "plt.bar(x,prob,width=0.4,alpha=0.6)\n",
                "\n",
                "# mu = 2，sigma = 1\n",
                "x= np.linspace(-3,3,20)\n",
                "prob=norm.pdf(x,2,1)\n",
                "\n",
                "plt.bar(x,prob,width=0.4,alpha=0.6)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "EF9E1851B2394DF6965870DE8DDC3B60",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "<div style=\"text-align: center;\">\n",
                "\n",
                "# 正态分布(Normal distribution)，当$\\sigma$不同时\n",
                "\n",
                "</div>"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 11,
            "metadata": {
                "collapsed": false,
                "id": "38867587F09347E78242868A78F57533",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/plain": [
                            "<BarContainer object of 20 artists>"
                        ]
                    },
                    "execution_count": null,
                    "metadata": {},
                    "output_type": "execute_result"
                },
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/38867587F09347E78242868A78F57533/ri58lmev06.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "# mu = 0，sigma = 0.5\n",
                "x= np.linspace(-3,3,20)\n",
                "prob=norm.pdf(x,0,0.5)\n",
                "\n",
                "plt.bar(x,prob,width=0.4,alpha=0.6)\n",
                "\n",
                "# mu = 0，sigma = 1\n",
                "x= np.linspace(-3,3,20)\n",
                "prob=norm.pdf(x,0,1)\n",
                "\n",
                "plt.bar(x,prob,width=0.4,alpha=0.6)\n",
                "\n",
                "# mu = 0，sigma = 2\n",
                "x= np.linspace(-3,3,20)\n",
                "prob=norm.pdf(x,0,2)\n",
                "\n",
                "plt.bar(x,prob,width=0.4,alpha=0.6)"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "5B33C496B99C4000AD904D6911646420",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "<div style=\"text-align: center;\">\n",
                "\n",
                "# t分布\n",
                "\n",
                "</div>"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {
                "id": "13A5191E258B471C9C35DB2220F8860D",
                "jupyter": {},
                "mdEditEnable": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "source": [
                "$f(x,v)=\\frac{\\Gamma ((v+1)/2)}{\\sqrt{\\pi v}\\gamma(v/2) }(1+x^2/v) ^{-(v+1)/2}$"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": 12,
            "metadata": {
                "collapsed": false,
                "id": "6234863F979B42D598FC564FA4021E57",
                "jupyter": {},
                "scrolled": false,
                "slideshow": {
                    "slide_type": "slide"
                },
                "tags": [],
                "trusted": true
            },
            "outputs": [
                {
                    "data": {
                        "text/html": [
                            "<img src=\"https://cdn.kesci.com/upload/rt/6234863F979B42D598FC564FA4021E57/ri58lmzlas.png\">"
                        ],
                        "text/plain": [
                            "<Figure size 432x288 with 1 Axes>"
                        ]
                    },
                    "metadata": {
                        "needs_background": "light"
                    },
                    "output_type": "display_data"
                }
            ],
            "source": [
                "fig, ax = plt.subplots(1, 1)\n",
                "\n",
                "df = 30\n",
                "\n",
                "# 在参数为0.3的情况下，伯努利分布的平均值m、方差v、峰度s和偏度k\n",
                "mean, var, skew, kurt = t.stats(df, moments='mvsk')\n",
                "\n",
                "\n",
                "x = np.linspace(t.ppf(0.01, df),\n",
                "                t.ppf(0.99, df), 100)\n",
                "\n",
                "ax.plot(x, t.pdf(x, df),\n",
                "       'r-', lw=5, alpha=0.6, label='t pdf')\n",
                "\n",
                "\n",
                "vals = t.ppf([0.001, 0.5, 0.999], df)\n",
                "np.allclose([0.001, 0.5, 0.999], t.cdf(vals, df))\n",
                "\n",
                "r = t.rvs(df, size=1000)\n",
                "ax.hist(r, density=True, histtype='stepfilled', alpha=0.2)\n",
                "ax.legend(loc='best', frameon=False)\n",
                "plt.show()\n"
            ]
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3.9.13 64-bit (microsoft store)",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "name": "python",
            "version": "3.9.13"
        },
        "orig_nbformat": 4,
        "vscode": {
            "interpreter": {
                "hash": "7698be9997df07547d4a08fe6d7a8ab77df170c0c470ab0ace6a1c514673cd42"
            }
        }
    },
    "nbformat": 4,
    "nbformat_minor": 2
}
